import logging
import os.path
import time
'''
日志级别： critical > error > warning > info > debug

format常用格式说明

%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径，其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
'''
# logging.basicConfig(level=logging.NOTSET) # 设置日志级别

# logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
# logging.debug('debug')
# logging.info('info')
# logging.warning('waring')
# logging.error('error')
# logging.critical('critical')

# 1.创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)  #log等级总开关
# 2.创建一个handle，用于写入日志文件
rq = time.strftime('%Y%m%d%H%M',time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd())
log_name = log_path + rq + '.logging'
logfile = log_name
fh = logging.FileHandler(logfile,mode='w')
fh.setLevel(logging.DEBUG)    # 输出file的log等级的开关
# 3.定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
fh.setFormatter(formatter)
# 4.将logger添加到handler里面
logger.addHandler(fh)

logging.debug('debug')
logging.info('info')
logging.warning('waring')
logging.error('error')
logging.critical('critical')